<%@ page import="com.vo.Students" %>
<%@ page import="com.factory.DAOFactory" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加学生信息</title>
</head>
<body>
<%
    // 设置请求编码
    request.setCharacterEncoding("UTF-8");

    // 获取表单提交的数据
    String studentId = request.getParameter("studentId");
    String studentName = request.getParameter("studentName");
    String studentPwd = request.getParameter("studentPwd");
    String gender = request.getParameter("gender");
    String classIdStr = request.getParameter("classId");

    // 检查班级号是否为空
    if (classIdStr == null || classIdStr.isEmpty()) {
        out.println("<script>alert('请选择班级！'); window.location.href='insertStudents.jsp';</script>");
        return;
    }
    int classId = Integer.parseInt(classIdStr);

    // 封装到 Students 对象中
    Students stu = new Students();
    stu.setStudentId(studentId);
    stu.setStudentName(studentName);
    stu.setStudentPwd(studentPwd);
    stu.setGender(gender);
    stu.setClassId(classId);

    boolean isSuccess = false;
    try {
        // 调用 DAO 插入学生信息
        isSuccess = DAOFactory.getStudentsDAOInstance().insertStudents(stu);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // 根据操作结果跳转页面
    if (isSuccess) {
        out.println("<script>alert('学生信息添加成功！'); window.location.href='showStudents.jsp';</script>");
    } else {
        out.println("<script>alert('学生信息添加失败！'); window.location.href='insertStudents.jsp';</script>");
    }
%>
</body>
</html>